package com.chainyoung.component_tmperson.ui.activity.dapp;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.bus.AppManagerEvent;
import com.chainyoung.common.bus.RxBus;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.event.BaseEvent;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.component_tmperson.di.component.DaggerPersonActivityComponent;
import com.chainyoung.component_tmperson.di.module.PersonActivityModule;
import com.chainyoung.component_tmperson.entity.BookMark;
import com.chainyoung.component_tmperson.ui.adapter.BookmarkListAdapter;
import com.chainyoung.component_tmperson.ui.adapter.LocalWalletListAdapter;
import com.chainyoung.component_tmperson.ui.mvp.contract.BrowserCollectionContract;
import com.chainyoung.component_tmperson.ui.mvp.presenter.BrowserCollectionPresenter;
import com.chainyoung.router.RouterConstants;
import com.chainyoung.router.RouterUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.simple.eventbus.EventBus;
import org.spongycastle.asn1.esf.SPuri;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.chainyoung.common.bus.AppManagerEvent.KILL_ALL;
import static com.chainyoung.common.constant.CommonStringConstants.URL;

public class BrowserCollectionActivity extends BaseActivity<BrowserCollectionPresenter> implements BrowserCollectionContract.View {


    @BindView(R2.id.rv_list)
    RecyclerView rvList;
    @BindView(R2.id.refresh_layout)
    SmartRefreshLayout refreshLayout;

    private List<BookMark> mData = new ArrayList<>();
    private BookmarkListAdapter mAdapter;


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPersonActivityComponent.builder()
                .appComponent(appComponent)
                .personActivityModule(new PersonActivityModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initUI() {
        setCurrentTitle("书签列表");
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        DividerItemDecoration itemDecoration = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
        itemDecoration.setDrawable(ContextCompat.getDrawable(mContext, R.drawable.shape_divider_gray));
        rvList.addItemDecoration(itemDecoration);
        mAdapter = new BookmarkListAdapter(mData);
        rvList.setLayoutManager(layoutManager);
        rvList.setAdapter(mAdapter);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                loadData();

            }
        });
        mAdapter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int p = (int) v.getTag();
//                if (v.getId() == R.id.iv_collect) {
//                    mData.get(p).setCollected(!mData.get(p).isCollected());
//                    mAdapter.notifyDataSetChanged();
//                } else
                if (v.getId() == R.id.rl_bookmark) {
                    Intent intent = new Intent(mContext, BrowserActivity.class);
                    intent.putExtra(URL, mData.get(p).getUrl());
                    startActivity(intent);
                    finish();
                }
            }
        });

        mAdapter.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                final int p = (int) v.getTag();
                android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(mContext);
                builder.setMessage(R.string.tmperson_confirm_cancel_bookmark)
                        .setPositiveButton(mContext.getString(R.string.confirm), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                delBookMark(mData.get(p).getId());
                                dialog.dismiss();
                            }
                        })
                        .setNegativeButton(mContext.getString(R.string.cancel), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                builder.create();
                builder.show();
                return true;
            }
        });
    }

    @Override
    protected boolean useLoadSir() {
        return true;
    }

    @Override
    protected Object registerTarget() {
        return refreshLayout;
    }

    @Override
    protected void onPageRetry(View v) {
        super.onPageRetry(v);
        loadData();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.tmperson_activity_browser_collection;

    }

    @Override
    protected void onResume() {
        super.onResume();
        loadData();
    }


    private String getDelIds() {
        List<String> ids = new ArrayList<>();
        for (BookMark item : mData) {
            if (!item.isCollected()) {
                ids.add(item.getId());
            }
        }
        return ids.isEmpty() ? "" : StringUtil.listToString(ids);
    }

    //--------------------------------------------------------网络请求--------------------------------------------------------------------

    private void loadData() {
        TreeMap<String, Object> map = new TreeMap<>();
        map.put("token", SPUtil.getString(LibConstant.TOKEN));
        mPresenter.getBookmarkList(map);
    }

    private void delBookMark(String delIds) {
        TreeMap<String, Object> map = new TreeMap<>();
        map.put("token", SPUtil.getString(LibConstant.TOKEN));
        map.put("id", delIds);
        mPresenter.delBookmarkList(map);
    }


    @Override
    public void onGetBookmarkListSuccessed(List<BookMark> data) {
        refreshLayout.finishRefresh();
        mData.clear();
        if (data != null) {
            mData.addAll(data);
        }
        mAdapter.notifyDataSetChanged();
        if (mData.isEmpty()) {
            showPageEmpty();
        } else {
            showPageContent();
        }

    }

    @Override
    public void onGetBookmarkListFailed() {
        refreshLayout.finishRefresh();
        showPageError();
    }

    @Override
    public void onDelBoomarkSuccessed(List<BookMark> data) {
        mData.clear();
        if (data != null) {
            mData.addAll(data);
        }
        mAdapter.notifyDataSetChanged();
        if (mData.isEmpty()) {
            showPageEmpty();
        } else {
            showPageContent();
        }
    }


}
